#include "LiquidCrystal.h"

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "WProgram.h"

LiquidCrystal::LiquidCrystal()
: _enable_pin(7), _sclk_pin(12), _data_rs_pin(13), _rw_pin(0)
{
  _mode = 2;
  initlcd();
}

LiquidCrystal::LiquidCrystal(uint8_t enable, uint8_t sclk, uint8_t data_rs)
: _enable_pin(enable), _sclk_pin(sclk), _data_rs_pin(data_rs), _rw_pin(0)
{
  _mode = 2;
  initlcd();
}

LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) :
  _data_rs_pin(rs), _rw_pin(rw), _enable_pin(enable), _sclk_pin(0)
{
  _mode = 1;
  
  _data_pins[0] = d0;
  _data_pins[1] = d1;
  _data_pins[2] = d2;
  _data_pins[3] = d3; 
  _data_pins[4] = d4;
  _data_pins[5] = d5;
  _data_pins[6] = d6;
  _data_pins[7] = d7; 
   
  for (int i = 0; i < 8; i++)
    pinMode(_data_pins[i], OUTPUT);
    
  initlcd();
}

LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) :
  _data_rs_pin(rs), _rw_pin(rw), _enable_pin(enable), _sclk_pin(0)
{
  _mode = 0;
  _data_pins[0] = d0;
  _data_pins[1] = d1;
  _data_pins[2] = d2;
  _data_pins[3] = d3; 
   
  for (int i = 0; i < 4; i++)
    pinMode(_data_pins[i], OUTPUT);

  initlcd();
}

void LiquidCrystal::initlcd()
{
  pinMode(_enable_pin, OUTPUT);
  pinMode(_data_rs_pin, OUTPUT);
  pinMode(_mode == 2 ? _sclk_pin : _rw_pin, OUTPUT);
  
  command(0x3C);  // function set: 8 bits, 2 line, 5x7 dots
  command(0x0C);  // display control: turn display on, cursor off, no blinking
  command(0x06);  // entry mode set: increment automatically, display shift, right shift
  clear();
}

void LiquidCrystal::send(uint8_t value, uint8_t mode)
{
  switch(_mode)
  {
    case 0: // four bit mode
      digitalWrite(_data_rs_pin, mode);
      digitalWrite(_rw_pin, LOW);
      
      for (int i = 0; i < 4; i++) {
        digitalWrite(_data_pins[i], (value >> (i + 4)) & 0x01);
      }
    
      digitalWrite(_enable_pin, HIGH);
      digitalWrite(_enable_pin, LOW);
    
      for (int i = 0; i < 4; i++) {
        digitalWrite(_data_pins[i], (value >> i) & 0x01);
      }

      digitalWrite(_enable_pin, HIGH);
      digitalWrite(_enable_pin, LOW);
      
      break;
    case 1: // eight bit mode
      digitalWrite(_data_rs_pin, mode);
      digitalWrite(_rw_pin, LOW);
      for (int i = 0; i < 8; i++) {
        digitalWrite(_data_pins[i], (value >> i) & 0x01);
      }

      digitalWrite(_enable_pin, HIGH);
      digitalWrite(_enable_pin, LOW);
      break;
    case 2: // eigth bit serial mode
      for (int i = 7; i >= 0; i--)
      {
        digitalWrite(_data_rs_pin, (value >> i) & 0x01);
        digitalWrite(_sclk_pin, LOW);
        digitalWrite(_sclk_pin, HIGH);
      }
      digitalWrite(_data_rs_pin, mode);
      
      digitalWrite(_enable_pin, HIGH);
      digitalWrite(_enable_pin, LOW);
      
      digitalWrite(_enable_pin, HIGH);
        
      break;
  }
}

void LiquidCrystal::clear()
{
  command(0x01);  // clear display, set cursor position to zero
  delayMicroseconds(2000);
}

void LiquidCrystal::home()
{
  command(0x02);  // set cursor position to zero
  delayMicroseconds(2000);
}

void LiquidCrystal::setCursor(int col, int row)
{
  int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  command(0x80 | (col + row_offsets[row]));
}

void LiquidCrystal::command(uint8_t value)
{
  send(value, LOW);
}

void LiquidCrystal::write(uint8_t value)
{
  send(value, HIGH);
}
